<?php
/**
 * +----------------------------------------------------------------------
 * | summer framework
 * +----------------------------------------------------------------------
 * | Copyright (c) 2020 https://github.com/suframe/think-admin All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: summer <806115620@qq.com>  2020/3/26 10:40
 * +----------------------------------------------------------------------
 */
declare (strict_types=1);

namespace app\mall\logic;

use app\mall\Events;
use app\mall\exceptions\UserException;
use app\mall\model\MallOrder;
use app\mall\model\MallUser;
use app\mall\validate\User;
use suframe\thinkAdmin\tools\Collection;
use think\exception\ValidateException;

/**
 * 用户注册
 * Class UserService
 * @package app\mall\service
 */
class UserLogic extends Base
{
    /**
     * 用户注册
     * @param array $info
     * @return bool
     */
    public function register(array $info): int
    {
        $allow = [
            'nickname', 'avatar', 'phone', 'mail', 'password'
        ];
        $info = (new Collection($info))->allowKeys($allow);
        try {
            validate(User::class)->batch(true)->check($info);
        } catch (ValidateException $e) {
            UserException::throws(UserException::$registerError, $e->getError(), 'register');
        }

        $model = MallUser::create($info);
        $rs = $model->save();
        if (!$rs) {
            UserException::throws(UserException::$registerError);
        }
        event(Events::USER_REGISTER, $model->id);
        return intval($model->id);
    }

    /**
     * 用户登录
     * @param string $token
     * @return bool
     */
    public function login(string $token): bool
    {

    }

    /**
     * 用户退出
     * @param string $token
     * @return bool
     */
    public function logout(string $token): bool
    {

    }

    /**
     * 用户信息更新
     * @param MallUser $mallUser
     * @return bool
     */
    public function updateInfo(MallUser $mallUser): bool
    {

    }

    /**
     * 用户等级更新
     * 跟进用户消费额度或者某种规则更新
     * @param MallUser $mallUser
     */
    public function updateLevel(MallUser $mallUser)
    {

    }

    /**
     * 用户积分增加
     * @param MallUser $mallUser
     * @param int $point
     * @return bool
     */
    public function addPoint(MallUser $mallUser, int $point): bool
    {

    }

    /**
     * 用户积分扣除
     * @param MallUser $mallUser
     * @param int $point
     * @return bool
     */
    public function deductionPoint(MallUser $mallUser, int $point): bool
    {

    }

    /**
     * 用户完成订单后，信息更新
     * 1. 用户奖励积分计算
     * 2. 用户总消费额度计算
     * 3. 用户等级更新
     * 4. 统计/运营相关数据更新
     * @param MallOrder $mallOrder
     * @return bool
     */
    public function afterOrderComplete(MallOrder $mallOrder): bool
    {

    }

}